<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '80px',
    }"
  >
    <div class="wrap-btn-group">
      <span
        class="wrap-btn wrap-success"
        v-if="$checkPermission(101)"
        @click="add"
        >添加菜单</span
      >
    </div>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'child', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="menuName" label="菜单名称"></el-table-column>
        <el-table-column prop="menuNo" label="菜单编码"></el-table-column>
        <el-table-column prop="url" label="菜单路由"></el-table-column>
        <el-table-column prop="icon" label="Icon名称"></el-table-column>
        <el-table-column
          prop="sort"
          label="排序"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="100px" :model="form" size="medium">
        <el-form-item label="菜单名称" required>
          <el-input
            v-model.trim="form.menuName"
            clearable
            maxlength="10"
            placeholder="请输入菜单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单编码" required>
          <el-input-number
            v-model="form.menuNo"
            controls-position="right"
            :min="1"
            :max="10000000000"
            style="width: 100%"
            placeholder="请输入菜单编码"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="菜单路由" required>
          <el-input
            v-model.trim="form.url"
            clearable
            maxlength="50"
            placeholder="请输入菜单路由"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" required>
          <el-input-number
            v-model="form.sort"
            :min="1"
            :max="50"
            :step="1"
            step-strictly
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-tree
            ref="tree"
            node-key="menuNo"
            :accordion="true"
            :data="menuData"
            show-checkbox
            :check-strictly="true"
            @check="check"
            class="isCheck"
            :props="{
              label: 'menuName',
              children: 'child',
            }"
            style="margin-top: 5px !important"
          ></el-tree>
        </el-form-item>
        <el-form-item label="Icon名称">
          <el-input
            v-model.trim="form.icon"
            clearable
            maxlength="20"
            placeholder="请输入Icon名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="按钮">
          <el-select
            clearable
            multiple
            size="medium"
            v-model="form.buttonArray"
            value-key="no"
            placeholder="请选择按钮"
            style="width: 100%"
          >
            <el-option
              v-for="item in buttonData"
              :key="item.no"
              :label="item.name"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="菜单名称">
          {{ lockData.menuName }}
        </el-descriptions-item>
        <el-descriptions-item label="菜单编码">
          {{ lockData.menuNo }}
        </el-descriptions-item>
        <el-descriptions-item label="按钮" :span="2">
          {{ noTotext(lockData.buttonArray) }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuData: [],
      buttonData: [],
      formSearch: {},
      tableData: [],
      visible: false,
      title: '添加菜单',
      form: { buttonArray: [] },
      isLook: false,
      lockData: { buttonArray: [] },
    }
  },
  created() {
    this.buttonList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.get('/admin/menu/page').then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    add() {
      //添加
      this.menuList()
      this.title = '添加菜单'
      this.form = { buttonArray: [] }
      this.visible = true
      this.$nextTick(function () {
        this.$refs.tree.setCheckedKeys([])
      })
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          this.$axios.get('/admin/menu/find?id=' + row.id).then((data) => {
            this.lockData = { buttonArray: [] }
            if (data.code == 200) {
              this.lockData = data.data
              this.isLook = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'edit':
          this.menuList()
          this.title = '编辑菜单'
          this.$axios.get('/admin/menu/find?id=' + row.id).then((data) => {
            this.form = { buttonArray: [] }
            if (data.code == 200) {
              this.form = data.data
              this.$nextTick(() => {
                this.$refs.tree.setCheckedKeys([data.data.parentNo])
              })
              this.visible = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios.get('/admin/menu/del?id=' + row.id).then((data) => {
                if (data.code == 200) {
                  this.itemList()
                  this.$message.success('操作成功！')
                } else {
                  this.$message.error(data.msg)
                }
              })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.menuName) {
        this.$message.error('请输入菜单名称')
        return
      }
      if (!this.form.menuNo) {
        this.$message.error('请输入菜单编码')
        return
      }
      if (!this.form.url) {
        this.$message.error('请输入菜单路由')
        return
      }
      if (!this.form.url) {
        this.$message.error('请输入菜单路由')
        return
      }
      const node = this.$refs.tree.getCheckedNodes()
      this.form.parentNo = node.length == 0 ? 0 : node[0].menuNo
      if (this.form.menuNo == this.form.parentNo) {
        this.$message.error('上级菜单不能是当前菜单')
        return
      }
      this.$axios.post('/admin/menu/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    menuList() {
      // 获取机构
      this.$axios.get('/admin/menu/page').then((data) => {
        this.menuData = []
        if (data.code == 200) {
          this.menuData = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    buttonList() {
      // 获取角色
      this.$axios.get('/admin/button/list').then((data) => {
        this.buttonData = []
        if (data.code == 200) {
          this.buttonData = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    noTotext(item) {
      const arr = []
      item.forEach((element) => {
        arr.push(element.name)
      })
      return arr.join(',')
    },
    check(a, b) {
      //监听checkbox改变事件，从而控制只选择一个节点
      if (b.checkedKeys.length > 0) {
        this.$refs.tree.setCheckedKeys([a.menuNo])
      }
    },
  },
}
</script>
